﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProfanityChecker
{
    public partial class frmPassword : Form
    {
        Settings m_settings;
        bool passwordset;
        public frmPassword(Settings settings)
        {
            InitializeComponent();
            m_settings = settings;
            passwordset = CheckForPassword();
            grpSetPassword.Visible = !passwordset;
            grpEnterPassword.Visible = passwordset;
        }

        private bool CheckForPassword()
        {
            if (string.IsNullOrEmpty(m_settings.Password))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (passwordset)
            {
                string pwd = txbxEnterPassword.Text;
                if (!pwd.Equals(m_settings.Password))
                {
                    MessageBox.Show("Sorry, the password you entered is incorrect.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                string set = txbxSetPassword.Text;
                string confirm = txbxConfirmPassword.Text;
                if (string.IsNullOrEmpty(set) ||
                    string.IsNullOrEmpty(confirm))
                {
                    MessageBox.Show("Sorry, you haven't entered anything for the password", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!set.Equals(confirm))
                {
                    MessageBox.Show("Sorry, the passwords you have entered don't match.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                m_settings.Password = set;                
            }
            this.DialogResult = DialogResult.OK;
            this.Close(); 
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmChangePassword chngpasswd = new frmChangePassword(m_settings);
            DialogResult result = chngpasswd.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }


    }
}
